<?php
/**
 * 飞信机器人--功能插件--手机归属地
 * 功能：
 * 数据表：
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/


class Mobile  extends CommonPlugin
{
	var $UUID='dcc5a720-543e-11de-8a39-0800200c9a66';
	
	public function Mobile(&$session, $pid, $extraPara="")
	{
		parent::__construct($session, $pid, $extraPara);
	}
	
	function getTips()
	{
		return "请输入要查询的手机号码。\n".$this->getFooter();
	}

	public function handle($input)
	{
		if( parent::handleInput($input)) return false;
		
		$url = "http://www.ip138.com:8080/search.asp";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 2);
		curl_setopt($ch, CURLOPT_POSTFIELDS, "action=mobile&mobile=$input");
		
		ob_start(); 
		curl_exec($ch);
		$content = ob_get_contents();
		ob_end_clean();
		
		curl_close($ch);

		$content = iconv('GBK', 'UTF-8', $content);
		$start = "<TD width=* align=\"center\" class=tdc2>";
		$end  = "</TD>";

		$startPos = 0;

		$arr = array();
		$flag = 0;
		while($flag<3)
		{
			$posStart = strpos($content, $start, $startPos)+strlen($start);
			$posEnd = strpos($content, $end, $posStart);

			$startPos = $posEnd+strlen($posEnd);
		
			$tmpStr = substr($content, $posStart, $posEnd-$posStart);
			$arr[] = $tmpStr;
			$flag++;
		}
		$mobile = $arr[1]." ".$arr[2];
		if( strstr($mobile,"TML 4.0"))
		{
			$mobile = "您输入的信息有误，未能查询到正确的结果";
		}
		return $mobile.$this->getFooter();
	}	
	
}

?>
